// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()

// 发送消息
const sendMsg = async function (params) {
  const wxContext = cloud.getWXContext()

  let chatId = params.chatId
  let content = params.content
  // 可能是 text、png、jpg、mp4
  let type = params.type 
  let headPortrait = params.headPortrait

  let data = {
    _openid: wxContext.OPENID,
    chatId: chatId,
    content: content,
    type: type,
    headPortrait: headPortrait,
    createTime: db.serverDate(),
    updateTime: db.serverDate(),
  }


  try {

    await db.collection('chatRecord').add({
      data: data
    })

    return true

  } catch (error) {

    console.log('appealSave error' + error)

    return false
  }

}

module.exports = sendMsg